{extend name="base" /}
{block name="body"}
<div class="layui-body">
    <!--tab标签-->
    <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title">
            <li class="layui-this">渠道游戏管理</li>
            <li class=""><a href="{:url('/admin/agent/add')}">添加渠道游戏</a></li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">

                <form class="layui-form layui-form-pane" action="{:url('/admin/agent')}" method="get">
                    <div class="layui-inline">
                        <label class="layui-form-label">关键词</label>
                        <div class="layui-input-inline">
                            <input type="text" name="keyword" value="{$keyword}" placeholder="请输入关键词" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline mleft">
                        <label class="layui-form-label">选择渠道</label>
                        <div class="layui-input-inline" style="width: 120px;">
                            <select name="cid" lay-filter="cid" id="cid" lay-search>
                                <option value="">请选择渠道</option>
                                {foreach name=":get_channel_list()" item="vo"}
                                    <option value="{$vo.id}"{if condition="input('cid') == $vo.id"}selected{/if}>{$vo.platform}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline mleft">
                        <label class="layui-form-label">选择游戏</label>
                        <div class="layui-input-inline" style="width: 120px;">
                            <select name="game" lay-filter="game" id="game" lay-search>
                                <option value="">请选择游戏</option>
                                {foreach name=":get_agent_game()" item="vo"}
                                    <option value="{$vo.id}"{if condition="input('game') == $vo.id"}selected{/if}>{$vo.name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn">搜索</button>
                    </div>
                </form>
                <hr>

                <table class="layui-table">
                    <thead>
                    <tr>
                        <th style="width: 45px;">ID</th>
						<th style="width: 45px;">渠道名称</th>
                        <th style="width: 60px;">游戏名称</th>
                        <th>申请时间</th>
						<th style="width: 80px;">代理状态</th>
                        <th>分成比例（%）</th>
                        <th>通道费（%）</th>
                        <th>渠道对接参数</th>
                        <th>发行参数</th>
{/*                        <th>游戏地址</th>*/}
{/*                        <th>通知地址</th>*/}
                        <th style="width: 25px;">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {foreach name="gameagent" item="vo"}
                    <tr>
                        <td>{$vo.id}</td>
                        <td>{:get_channel_name($vo['cid'])}</td>
                        <td>{:get_game_filed($vo['gid'],'name')}</td>
                        <td>{$vo.applytime|date='Y-m-d H:i:s'}</td>
						<td class="layui-form">
						    <div class="layui-form layui-form-item">
  								<input lay-skin="switch" lay-filter="status" type="checkbox" {eq name="$vo.status" value="1"}checked="" name="open"{else/}name="close"{/eq} lay-skin="switch" lay-text="开启|关闭" table-value="gameagent" field-value="status" key-value="id" id-value="{$vo.id}" status-value="{$vo.status}">
							</div>
						</td>
                        <td>{$vo.fencheng}</td>
                        <td>{$vo.tongdao}</td>
                        <td><a href="{:url('/admin/agent/extData',['id'=>$vo['id']])}" class="layui-btn layui-btn-mini">设置</a></td>

                        <td><span onclick="channelGameNotify(this)" data-game_id="{$vo.gid}" data-game_name="{:get_game_filed($vo['gid'],'name')}" data-login_url="{$vo.login_url}" data-game_notify="{:get_game_notify($vo['gid'],$vo['cid'])}"
                                  data-channel_name="{:get_channel_name($vo['cid'])}" class="layui-btn layui-btn-normal layui-btn-mini">查看</span></td>
                        <td>
                            <a href="{:url('/admin/agent/edit',['id'=>$vo['id']])}" class="layui-btn layui-btn-mini">编辑</a>
                        </td>
                    </tr>
                    {/foreach}
                    </tbody>
                </table>
                <!--分页-->
                {$gameagent|raw}
            </div>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script>
//Demo
;!function(){
  var layer = layui.layer
  ,form = layui.form;
  
  //监听开启开关
  form.on('switch(swbtn)', function(data){
    var url  = $(this).attr('data-url');
	var type = $(this).attr('data');
	var date = $(this).attr('data2');
	var status = this.checked ? 1:0;
	$.post(url,{id:date,type:type,status:status},function(info){
	    layer.msg(info.msg);
		})
  });
  
};
layui.use('form', function(){
  var form = layui.form;
      laydate = layui.laydate;
	form.on('switch(status)',function(data){
    	var id=($(data.elem).attr('id-value'));
	    var table=($(data.elem).attr("table-value"));
        var field=($(data.elem).attr("field-value"));
        var key=($(data.elem).attr("key-value"));
        var url = "{:url('/admin/checkstatus')}";
       $.get(url,{id:id,table:table,field:field,key:key},function(data){
    		window.location.reload();
    	});
	});
});

// 查看渠道对接参数

function gameAgentExtData(el){
    var id = $(el).attr('data-id');
    var url = "{:url('/admin/agent/extData',['id'=>$vo['id']])}";
    layer.open({
        type: 1,
        title: '发行对接参数',
        area: ['540px', '430px'],
        shadeClose: true, //点击遮罩关闭
        btn: ['复制参数', '关闭'], // 自定义按钮
        btnAlign: 'c', // 按钮居中
        yes: function(index){
            copyToClipboard(text);
            layer.msg('复制成功');
        },
        btn2: function(index){
            // 关闭按钮回调
            layer.close(index);
        },
        content: url
    });
}

//查看发行参数
function channelGameNotify(element) {
    var game_id = $(element).attr('data-game_id');
    var game_name = $(element).attr('data-game_name');
    var channel_name = $(element).attr('data-channel_name');
    var login_url = $(element).attr('data-login_url');
    var game_notify = $(element).attr('data-game_notify');

    var text = '游戏ID：'+game_id+'\n游戏名称：'+game_name+'\n发行渠道：'+channel_name+'\n登录地址：'+login_url+'\n支付回调地址：'+game_notify+'\n';
    layer.open({
        type: 1,
        title: '发行对接参数',
        area: ['540px', '430px'],
        shadeClose: true, //点击遮罩关闭
        btn: ['复制参数', '关闭'], // 自定义按钮
        btnAlign: 'c', // 按钮居中
        yes: function(index){
            copyToClipboard(text);
            layer.msg('复制成功');
        },
        btn2: function(index){
            // 关闭按钮回调
            layer.close(index);
        },
        content: '<div style="padding: 20px;">' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">游戏id</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="game_id" value="'+game_id+'" placeholder="游戏ID" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">游戏名称</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="game_name" value="'+game_name+'" placeholder="游戏名称" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">发行渠道</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="channel_name" value="'+channel_name+'" placeholder="发行渠道" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">登录地址</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="login_url" value="'+login_url+'" placeholder="登录地址" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '<div class="layui-form-item">' +
                '<label class="layui-form-label">支付回调地址</label>' +
                '<div class="layui-input-block">' +
                '<input type="text" name="game_notify" value="'+game_notify+'" placeholder="支付回调地址" class="layui-input" readonly>' +
                '</div>' +
                '</div>' +

                '</div>',

    });
}

function copyToClipboard(text) {
    const textarea = document.createElement('textarea');
    textarea.value = text;
    document.body.appendChild(textarea);
    textarea.select();
    document.execCommand('copy');
    document.body.removeChild(textarea);
}

</script>
{/block}
